﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using OAuth;
using System.IO;
using Entity;
using System.Reflection;
using BusinessLogic;
using System.Net;

namespace Avatar
{
    public partial class LoginForm : Form
    {
        private bool saveCredential = true;
        private static BusinessLogic.BusinessLogic bzlogic = new BusinessLogic.BusinessLogic();
        private UserCredential credential = UserCredentialLogic.LoadUserCredentialInfo();

        UserCredential Credential
        {
            get 
            {
                if (credential == null)
                {
                    credential = new UserCredential();
                }
                return credential; 
            }
            set 
            {
                if (credential != null)
                    throw new NotSupportedException();
            }
        }
        public LoginForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                bzlogic.userCredentialLogic.GetRequestToken(this.Credential);
            }
            catch (WebException ex)
            {
                MessageBox.Show(ex.Message);
            }
            //去 授权页面
            this.tabControl1.SelectTab("tabPage2");
            this.webBrowser1.Navigate(bzlogic.userCredentialLogic.AuthorizationUri + Credential.RequestToken);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (string.IsNullOrEmpty(this.Credential.RequestToken) || string.IsNullOrEmpty(this.Credential.RequestToken) || string.IsNullOrEmpty(this.Credential.AccessToken) || string.IsNullOrEmpty(this.Credential.AccessTokenSecret))
            {
                MessageBox.Show("登录未完成，可能授权未完成", "登录失败", MessageBoxButtons.OK);
            }
        }

        private void rememberMe_CheckStateChanged(object sender, EventArgs e)
        {
            if (rememberMe.CheckState == CheckState.Checked)
            {
                saveCredential = true;
            }
            else
            {
                saveCredential = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                //用 Request Token 换取 Access Token
                //if (!string.IsNullOrEmpty(this.Credential.RequestToken) && !string.IsNullOrEmpty(this.Credential.RequestTokenSecret))
                UserCredentialLogic.GetAccessToken(this.Credential);
            }
            catch (WebException ex)
            {
                throw;
            }

            if (saveCredential)
            {
                //认证成功
                UserCredentialLogic.SaveUserCredentialInfo(this.Credential);
            }
            Globals.ThisAddIn.credential = this.Credential;
            MessageBox.Show("登录成功！");
        }
    }
}
